package me.youline.dataServer.service.support;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;

public class MsgBuilder {

	public static ErrorMsgBuilder error() {
		return ErrorMsgBuilder.init();
	}

	public static SuccessMsgBuilder success() {
		return SuccessMsgBuilder.init();
	}
	
	
	
	public static class SuccessMsgBuilder {
		
		private SuccessMsg  msg = new SuccessMsg();
		
		private SuccessMsgBuilder() {
		}
		
		public static SuccessMsgBuilder init(){
			return new SuccessMsgBuilder();
		}
		
		public SuccessMsgBuilder addObj(String name,Object value){
			msg.getData().put(name, value);
			return this;
		}
		
		public String toJSONString() {
			return JSON.toJSONString(this.msg);
		}
		

		private static class SuccessMsg {

			private final String status = "success";

			private Map<String, Object> data = new LinkedHashMap<String, Object>();

			public Map<String, Object> getData() {
				return data;
			}

			public String getStatus() {
				return status;
			}

		}

	}
	
	
	public static class ErrorMsgBuilder {
		
		private ErrorMsg errorMsg = new ErrorMsg();

		private ErrorMsgBuilder() {
		}

		public static ErrorMsgBuilder init() {
			return new ErrorMsgBuilder();
		}

		public ErrorMsgBuilder errorCode(String code) {
			this.errorMsg.setErrorCode(code);
			return this;
		}

		public ErrorMsgBuilder reqUrl(String url) {
			this.errorMsg.setRequestUrl(url);
			return this;
		}

		public ErrorMsgBuilder errorMsg(String msg) {
			this.errorMsg.setErrorMsg(msg);
			return this;
		}

		public String toJSONString() {
			return JSON.toJSONString(this.errorMsg);
		}

		private static class ErrorMsg implements Serializable{

			private static final long serialVersionUID = -255691924021853115L;

			private String errorCode = "1";

			private String requestUrl = "";

			private String errorMsg = "";

			public String getErrorCode() {
				return errorCode;
			}

			public void setErrorCode(String errorCode) {
				this.errorCode = errorCode;
			}

			public String getRequestUrl() {
				return requestUrl;
			}

			public void setRequestUrl(String requestUrl) {
				this.requestUrl = requestUrl;
			}

			public String getErrorMsg() {
				return errorMsg;
			}

			public void setErrorMsg(String errorMsg) {
				this.errorMsg = errorMsg;
			}

		}
	}


}
